package com.novellotus.iov.carvice.retrofit;


import android.content.Context;

import com.ljd.retrofit.progress.ProgressHelper;
import com.novellotus.iov.carvice.BuildConfig;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Rich on 2017/5/8.
 * 单例获取Retrofit实例
 */


public class RetrofitFactory {

    private static final long TIMEOUT = 15;
    /**
     * 测试环境 接口
     */
//     public static String baseUrl = "http://192.168.1.100:8282/api/2.0/";
//    public static String baseUrl = "http://192.168.1.11:8484/api/2.0/";
    public static String baseUrl = "http://192.168.1.102:8282/api/2.0/";
//      public static String baseUrl = "http://192.168.1.200:8282/api/2.0/";
    /**
     * 正式环境 接口
     */
//    public static String baseUrl = "http://app.novellotus.com/api/2.0/";


    /**
     * 正式环境 http://get.wokooyun.com/ne/
     */
    public static String imgUrl = "http://get.wokooyun.com/ne/";
    /**
     * 测试 http://get.wokooyun.com/netest/
     */
//    public static String imgUrl = "http://get.wokooyun.com/netest/";

    /**
     * mqtt  正式环境
     */
    public static String host = "tcp://iot.wokooyun.com";

    /**
     * mqtt  测试环境
     */
    //  public static String host = "tcp://192.168.1.20:1883";
    private static HttpLoggingInterceptor setLog() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                //  XLog.json(message);
                MyUtils.logInformation(message);
            }
        });
        if (BuildConfig.DEBUG) {
            //显示日志
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }

        return loggingInterceptor;
    }

    //设置超时时间30秒，token或者cookies
    private static OkHttpClient httpClient = new OkHttpClient.Builder()
            .addInterceptor(setLog())
            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
            .build();

    private static RetrofitApi retrofitApi = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(httpClient)
            .baseUrl(baseUrl)
            .build()
            .create(RetrofitApi.class);

    public static RetrofitApi getInstance() {
        return retrofitApi;
    }


    public static RetrofitApi getDownLoadInstance(Context context, String tittle, String des) {
        OkHttpClient.Builder b = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor());
        OkHttpClient.Builder builder = ProgressHelper.addProgress(tittle, des, b, context);
        RetrofitApi downRetrofitApi = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(builder.build())
                .baseUrl(baseUrl)
                .build()
                .create(RetrofitApi.class);
        return downRetrofitApi;
    }

}
